<?php

namespace app\index\service;

use app\common\model\Area;
use app\common\model\Consulting;
use app\common\model\LawCase;
use app\common\model\Member;
use app\index\dao\LawCaseDao;
use app\index\model\UM as UM;
use think\exception\ValidateException;

/**
 * Class LawCaseService
 * @package app\consulting\service
 */
class LawCaseService
{
    // dao
    private LawCaseDao $lawCaseDao;

    public function __construct(LawCaseDao $lawCaseDao)
    {
        $this->lawCaseDao = $lawCaseDao;
    }

    /**
     * 新增咨询内案源
     * @param UM $user
     * @param array $params
     * @return  LawCase
     */
    public function addConsultingLawCase(UM $user, array $params): LawCase
    {
        $consultingId = $params['consultingId'] ?? null;
        $categoryId = $params['categoryId'] ?? null;
        $moneyId = $params['moneyId'] ?? null;
        $daysId = $params['daysId'] ?? null;
        $provinceId = $params['provinceId'] ?? null;
        $cityId = $params['cityId'] ?? null;
        $alwaysLive = $params['alwaysLive'] ?? null;
        $isSelf = $params['isSelf'] ?? null;
        $employLawyer = $params['employLawyer'] ?? null;


        if (!is_id($consultingId)) {
            throw new ValidateException('请选择一个聊天');
        }
        if (!is_id($categoryId)) {
            throw new ValidateException('请选择一个案件类型');
        }
        if (!is_id($cityId)) {
            throw new ValidateException('请选择所在城市');
        }
        if (!is_id($daysId)) {
            throw new ValidateException('请选择案发天数');
        }
        if (!is_id($moneyId)) {
            throw new ValidateException('请选择涉案金额');
        }
        if (!is_numeric($alwaysLive)) {
            throw new ValidateException('请选择是否为常住城市');
        }
        if (!is_numeric($isSelf)) {
            throw new ValidateException('请选择是否为当事人');
        }
        if (!is_numeric($employLawyer)) {
            throw new ValidateException('请选择是否为聘请律师');
        }

        if (!is_id($provinceId) && is_id($cityId)) {
            $city = Area::where('id', $cityId)->find();
            if ($city) {
                $provinceId = $city->pid;
            }
        }

        $alwaysLive = $alwaysLive == 1 ? 1 : 0;
        $isSelf = $isSelf == 1 ? 1 : 0;
        $employLawyer = $employLawyer == 1 ? 1 : 0;

        // 咨询
        $consulting = Consulting::where('id', $consultingId)->find();
        if (is_null($consulting)) {
            throw new ValidateException('该聊天不存在或已被删除');
        }

        if (!is_id($consulting->questionId)) {
            throw new ValidateException('该聊天还没有提问，请在提问后补充案源信息');
        }

        if ($user->id != $consulting->memberId && $user->id != $consulting->lawyerId) {
            throw new ValidateException('您不能补充该聊天的案源信息');
        }

        // 已提交过了
        $repeat = LawCase::where('questionId', $consulting->questionId)->find();
        if ($repeat) {
            throw new ValidateException('该咨询已经补充提交了案源信息，请勿重复提交');
        }

        $phone = Member::where('id', $consulting->memberId)->value('phone');

        $caseParams = [
            'questionId' => $consulting->questionId,
            'memberId' => $consulting->memberId,
            'creatorId' => $user->id,
            'creatorMemberType' => $user->type,
            'ask' => $consulting->ask,
            'categoryId' => $categoryId,
            'moneyId' => $moneyId,
            'provinceId' => $provinceId,
            'cityId' => $cityId,
            'phone' => $phone,
            'alwaysLive' => $alwaysLive,
            'isSelf' => $isSelf,
            'daysId' => $daysId,
            'employLawyer' => $employLawyer,
            // 提交人提成20%
            'creatorShareRate' => 0.2,
        ];

        $record = $this->lawCaseDao->insertLawCase($caseParams);

        // 触发咨询变化事件
        event('ConsultingUpdated', $consulting);

        return $record;
    }
}
